<template>
  <div>
    <el-dialog
      :visible.sync="visible"
      title="编辑银行卡信息"
      width="60%"
      :before-close="handleClose"
    >
      <el-form
        :model="dispatchForm"
        :rules="dispatchFormRules"
        ref="dispatchFormRef"
        label-width="110px"
      >
        <el-form-item label="开户银行" prop="bank_id">
          <el-select
            v-model="dispatchForm.bank_id"
            placeholder="请选择"
            filterable
            clearable
          >
            <el-option
              v-for="item in selectData.get_bank"
              :key="item.id"
              :label="item.name + '【' + item.id + '】'"
              :value="item.id"
            >
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="支行信息" prop="merchant">
          <el-input  v-model="dispatchForm.merchant" placeholder="请输入支行信息"></el-input>
        </el-form-item>
        <el-form-item label="账户名称" prop="account">
          <el-input  v-model="dispatchForm.account" placeholder="请输入账户名称"></el-input>
        </el-form-item>
        <el-form-item label="银行账号" prop="card_no">
          <el-input  v-model="dispatchForm.card_no" placeholder="请输入银行账号"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="closeDialog">取 消</el-button>
        <el-button
          class="theme-background-color"
          @click="saveDispatchInfo"
          :disabled="isDisabled"
          >保 存</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
export default {

  props: {
    EditBankCardShow: {
      type: Boolean,
      default: function () {
        return {}
      }
    },
    hospital_id: {
      type: Number,
      default: function () {
        return {}
      }
    },
    card_info: {
      type: Object,
      default: function () {
        return {}
      }
    }
  },
  data() {
    return {
      visible: false,
      dispatchForm: {
        bank_id: "",
        card_no: "",
        merchant: "",
        account: "",
        id: ""
      },
      dispatchFormRules: {
        bank_id: [{ required: true, message: '请输入银行名称', trigger: 'blur' }],
        card_no: [{ required: true, message: '请输入卡号', trigger: 'blur' }],
        merchant: [{ required: true, message: '请输入支行信息', trigger: 'blur' }],
        account: [{ required: true, message: '请输入帐户名称', trigger: 'blur' }]
      },
      isDisabled: false,
      selectData: {}
    }
  },
  created() {
   this.getBank()
  },
  methods: {
    // 左上角关闭
    handleClose(done) {
      this.$confirm('确认关闭？')
        .then((_) => {
          this.$emit('update:EditBankCardShow', false)
          done()
        })
        .catch((_) => {})
    },
    // 取消按钮
    closeDialog() {
      this.$emit('update:EditBankCardShow', false)
    },
    async getBank() {
      const { data: res } = await this.$http.post('/admin/Data/find', {
        code: 'get_bank'
      })
      if (res.meta.status !== 200) {
        return this.$message.error(res.meta.msg)
      }
      this.selectData = res.data
    },
    // 保存按钮
    async saveDispatchInfo() {
      this.$refs.dispatchFormRef.validate(async (valid) => {
        if (!valid) return
        this.isDisabled = true
        const { data: res } = await this.$http.post(
          '/admin/Hospital/addBankCard',
          this.dispatchForm
        )
        this.isDisabled = false
        if (res.meta.status !== 200) {
          return this.$message.error(res.meta.msg)
        }
        this.$message.success('修改成功')
        this.visible = false
        this.$emit('update:EditBankCardShow', false)
        parent.location.reload()
      })
    }
  },
  watch: {
    EditBankCardShow() {
      this.visible = this.EditBankCardShow
      this.dispatchForm = this.card_info
    }
  }
}
</script>

<style lang="less" scoped>
.el-form {
  width: 80%;
  margin-left: 15px;
  .el-select {
    width: 100%;
  }
}
</style>
